﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Security;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using WPS.Log;

namespace WPS.SDK.http
{
    public class Request
    {
        public Response Post(string url, string param)
        {
            LogHelper.Info("url：" + url + ";<br/>param：" + param);
            string responseData = String.Empty;
            HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
            request.Method = "POST";
            Encoding encoding = Encoding.UTF8;
            byte[] byteArray = Encoding.UTF8.GetBytes(param);
            request.ContentLength = byteArray.Length;
            request.ContentType = "application/json";
            request.Accept = "application/x-www-form-urlencode;charset=UTF-8";
            Stream reqStream = request.GetRequestStream();
            reqStream.Write(byteArray, 0, byteArray.Length);
            reqStream.Close();
            HttpWebResponse response = request.GetResponse() as HttpWebResponse;
            return new Response(response);
        }

        public Response Get(string url)
        {
            LogHelper.Info("url：" + url );
            HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
            request.Method = "GET";
            request.ContentType= "application/json";
            HttpWebResponse response = request.GetResponse() as HttpWebResponse;
            return new Response(response); 
        }

        public Response Put(HttpWebRequest request, HttpPostedFileBase file) {
            int fileLength = file.ContentLength;
            byte[] buffer = new byte[fileLength];
            file.InputStream.Read(buffer, 0, file.ContentLength);
            Stream reqStream = request.GetRequestStream();
            reqStream.Write(buffer, 0, buffer.Length);
            reqStream.Close();
            HttpWebResponse response = request.GetResponse() as HttpWebResponse;
            return new Response(response);
        }

        public Response Delete(string url, string param)
        {
            LogHelper.Info("url：" + url + ";<br/>param：" + param);
            string responseData = String.Empty;
            HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
            request.Method = "DELETE";
            Encoding encoding = Encoding.UTF8;
            byte[] byteArray = Encoding.UTF8.GetBytes(param);
            request.ContentLength = byteArray.Length;
            request.ContentType = "application/json";
            request.Accept = "application/x-www-form-urlencode;charset=UTF-8";
            Stream reqStream = request.GetRequestStream();
            reqStream.Write(byteArray, 0, byteArray.Length);
            reqStream.Close();
            HttpWebResponse response = request.GetResponse() as HttpWebResponse;
            return new Response(response);
        }

    }

}
